# coding: utf-8

from igraph import Graph as Digraph
from graph import Graph
from layout import SugiyamaLayout
from draw import DrawingOptions, draw_svg_image


def create_graph_from_file(filename):
    # Create an empty graph
    g = Graph(Digraph(directed=True))

    # Open the file and read line by line
    with open(filename, "r") as file:
        for line in file:
            # Split the line into components and take the first two as nodes
            parts = line.strip().split()
            node1, node2 = int(parts[0]), int(parts[1])
            # Add an edge between these nodes
            g.add_edge(node1, node2)
    return g


"""
# 1.txt
# 遗传算法中的 一个小图
# 2.txt

# 流体网络简化的图
# 3.txt

# 自己画的另一个复杂图
# 4.txt

# 拓扑数据3的
# 5.txt

# 拓扑数据4
# 6.txt
"""


def main():
    # 读取文件创建有向图
    g = create_graph_from_file("data/test.txt")
    layout = SugiyamaLayout(g, attrs=None)
    c = layout.run()
    # print("将虚拟节点去掉的交叉边", c)
    # print('===========', c.edges())
    # 打印节点坐标
    # for p in c.edges():
    #     print(p.points[0])
    # for x, y in p.points:
    #     print(x, y)
    opts = DrawingOptions()
    # opts.labels[0] = "a"
    # opts.labels[1] = "b"
    # opts.labels[2] = "c"

    # opts.colors[0] = "blue"
    # opts.colors[1] = "green"
    # opts.edge_colors[(0, 1)] = "red"
    draw_svg_image(layout, opts, "test.svg")


if __name__ == "__main__":
    main()
